package server

import (
	"chitchat_http/model"
	"chitchat_http/util"
	"errors"
	"fmt"
	"github.com/doujunyu/gogo/gogo_log"
	"github.com/doujunyu/gogo/sql_aid"
	"github.com/doujunyu/gogo/utility"
	"os"
	"strings"
	"time"
)

// BbsByRelease 发布动态
func BbsByRelease(userId int64, title string, content string, picture string, typeId int64) error {
	t := time.Now().Format(time.RFC3339)
	insert := make(map[string]interface{})
	insert["user_id"] = userId
	insert["title"] = title
	insert["content"] = content
	insert["discuss_number"] = 0
	insert["praise_number"] = 0
	insert["time_c"] = t
	insert["time_u"] = t
	insert["status"] = model.BbsByStatusNormal
	insert["type_id"] = typeId
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByRelease", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	defer tx.Rollback()
	//动态评论添加数据
	insertSql, args := sql_aid.PgTable(model.TableByBbs).InsertByMap(insert)
	var bbsId int64
	err = tx.QueryRow(insertSql+" RETURNING id", args...).Scan(&bbsId)
	if err != nil {
		gogo_log.Error("server_BbsByRelease", "bbs添加数据", err.Error())
		return errors.New("操作失败")
	}
	//动态评论图片添加
	if len(picture) != 0 { //picture非空，且切片
		picturePath := strings.Replace(picture,os.Getenv("SERVER_HTTP"),"",-1)//去除域名
		pictureSplit := strings.Split(picturePath, ",")//转成切片
		insertSplit := make([]map[string]interface{}, 0)
		for _, v := range pictureSplit {
			pictureNewPath := strings.Replace(v,"common",model.BbsPictureByFilePath,-1)//从common移出后则不会再删除
			_,_ = utility.CopyFile("."+pictureNewPath,"."+v)
			pictureMap := make(map[string]interface{})
			pictureMap["bbs_id"] = bbsId
			pictureMap["picture"] = pictureNewPath
			insertSplit = append(insertSplit, pictureMap)
		}
		insertPictureSql, args := sql_aid.PgTable(model.TableByBbsPicture).InsertAllByMap(&insertSplit)
		_, err = tx.Exec(insertPictureSql, args...)
		if err != nil {
			gogo_log.Error("server_BbsByRelease", "bbs_picture添加数据", err.Error())
			return errors.New("操作失败")
		}
	}
	tx.Commit()
	return nil
}

// BbsByDelete 删除动态
func BbsByDelete(userId int64, bbsDd int64) error {
	//验证用户是否可以删除
	bbsSql, args := sql_aid.PgTable(model.TableByBbs).WhereId(bbsDd).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsSql, args...))
	if err != nil || len(bbsData) == 0 {
		gogo_log.Error("server_BbsByDelete", "数据不存在", err.Error())
		return errors.New("数据不存在")
	}
	if bbsData[0]["user_id"].(int64) != userId {
		return errors.New("这条帖子不属于您，您无法删除")
	}
	t := time.Now().Format(time.RFC3339)
	//删除评论
	update := make(map[string]interface{})
	update["time_u"] = t
	update["status"] = model.BbsByStatusDelete
	bbsSql, args = sql_aid.PgTable(model.TableByBbs).WhereId(bbsDd).UpdateByMap(update)
	_, err = model.PgSql.Exec(bbsSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByDelete", "操作失败", err.Error())
		return errors.New("操作失败")
	}
	return nil
}

// BbsByPraiseInc 动态点赞+1
func BbsByPraiseInc(userId int64, bbsId int64) error {
	//检测是否已经点赞
	bbsSql, args := sql_aid.PgTable(model.TableByBbsPraise).Where("user_id = ?", userId).Where("bbs_id  = ?", bbsId).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsSql, args...))
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "动态点赞查询错误", err.Error())
		return errors.New("数据不存在")
	}
	if len(bbsData) >= 1 {
		return errors.New("您已经赞过，无需重复点击")
	}
	//进行点赞操作
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "事务错误", err.Error())
		return errors.New("执行失败")
	}
	defer tx.Rollback()
	t := time.Now().Format(time.RFC3339)
	bbsInsert := make(map[string]interface{})
	bbsInsert["user_id"] = userId
	bbsInsert["bbs_id"] = bbsId
	bbsInsert["time_c"] = t
	bbsInsertSql, args := sql_aid.PgTable(model.TableByBbsPraise).InsertByMap(bbsInsert)
	_, err = tx.Exec(bbsInsertSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	//对应动态点赞数+1
	bbsIncSql, args := sql_aid.PgTable(model.TableByBbs).Where("id = ?", bbsId).Inc("praise_number", 1).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsIncSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "对应动态点赞数+1失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// BbsByPraiseDec 动态点赞-1
func BbsByPraiseDec(userId int64, bbsId int64) error {
	//检测是否已经点赞
	bbsSql, args := sql_aid.PgTable(model.TableByBbsPraise).Where("user_id = ?", userId).Where("bbs_id  = ?", bbsId).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsSql, args...))
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "动态点赞查询错误", err.Error())
		return errors.New("数据不存在")
	}
	if len(bbsData) == 0 {
		return errors.New("您还未关注，无需取消")
	}
	//进行点赞操作
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "事务错误", err.Error())
		return errors.New("执行失败")
	}
	defer tx.Rollback()
	bbsInsertSql, args := sql_aid.PgTable(model.TableByBbsPraise).Where("user_id = ?", userId).Where("bbs_id = ?", bbsId).Delete()
	_, err = tx.Exec(bbsInsertSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "删除失败", err.Error())
		return errors.New("执行失败")
	}
	//对应动态点赞数-1
	bbsIncSql, args := sql_aid.PgTable(model.TableByBbs).Where("id = ?", bbsId).Dec("praise_number", 1).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsIncSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "-1操作失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// BbsDiscussByInsert 评论动态
func BbsDiscussByInsert(userId int64, bbsId int64, content string) error {
	t := time.Now().Format(time.RFC3339)
	discussInsert := make(map[string]interface{})
	discussInsert["bbs_id"] = bbsId
	discussInsert["user_id"] = userId
	discussInsert["content"] = content
	discussInsert["praise_number"] = 0
	discussInsert["parent_id"] = 0
	discussInsert["parent_user_id"] = 0
	discussInsert["parent_level"] = 1
	discussInsert["parent_path"] = "-"
	discussInsert["status"] = model.BbsDiscussByStatusNormal
	discussInsert["time_c"] = t
	discussInsert["time_u"] = t
	discussInsert["super_parent_id"] = 0
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsDiscussByInsert", "打开数据库失败", err.Error())
		return errors.New("操作失败")
	}
	defer tx.Rollback()
	discussInsertSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).InsertByMap(discussInsert)
	_, err = tx.Exec(discussInsertSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsDiscussByInsert", "打开数据库失败", err.Error())
		return errors.New("操作失败")
	}
	//动态评论数据+1
	bbsIncSql, args := sql_aid.PgTable(model.TableByBbs).Where("id = ?", bbsId).Inc("discuss_number", 1).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsIncSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsDiscussByInsert", "数据+1失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// BbsDiscussByReply 评论回复
func BbsDiscussByReply(userId int64, parentId int64, content string) error {
	bbsDiscussSql, arge := sql_aid.PgTable(model.TableByBbsDiscuss).Where("id = ?", parentId).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsDiscussSql, arge...))
	if err != nil || len(bbsData) == 0 {
		gogo_log.Error("server_BbsByDelete", "数据不存在", err.Error())
		return errors.New("数据不存在")
	}

	t := time.Now().Format(time.RFC3339)
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByDelete", "打开事务错误", err.Error())
		return errors.New("内部错误")
	}
	defer tx.Rollback()
	bbsDiscussInsert := make(map[string]interface{})
	bbsDiscussInsert["bbs_id"] = bbsData[0]["bbs_id"].(int64)
	bbsDiscussInsert["user_id"] = userId
	bbsDiscussInsert["content"] = content
	bbsDiscussInsert["praise_number"] = 0
	bbsDiscussInsert["parent_id"] = parentId
	bbsDiscussInsert["parent_user_id"] = bbsData[0]["user_id"].(int64)
	bbsDiscussInsert["parent_level"] = bbsData[0]["parent_level"].(int64) + 1
	bbsDiscussInsert["parent_path"] = fmt.Sprintf("%v%v-", bbsData[0]["parent_path"].(string), parentId)
	bbsDiscussInsert["status"] = model.BbsDiscussByStatusNormal
	bbsDiscussInsert["time_c"] = t
	bbsDiscussInsert["time_u"] = t
	var super_parent_id int64
	if bbsData[0]["super_parent_id"].(int64) == 0 {
		super_parent_id = bbsData[0]["id"].(int64)
	} else {
		super_parent_id = bbsData[0]["super_parent_id"].(int64)
	}
	bbsDiscussInsert["super_parent_id"] = super_parent_id
	//进行添加评论
	bbsDiscussInsertSql, arge := sql_aid.PgTable(model.TableByBbsDiscuss).InsertByMap(bbsDiscussInsert)
	_, err = tx.Exec(bbsDiscussInsertSql, arge...)
	if err != nil {
		gogo_log.Error("server_BbsByDelete", "数据不存在", err.Error())
		return errors.New("数据不存在")
	}
	//动态评论+1
	bbsIncSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Where("id = ?", parentId).Inc("discuss_number", 1).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsIncSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsDiscussByInsert", "数据+1失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// BbsDiscussByDelete 删除评论
func BbsDiscussByDelete(userId int64, bbsDiscussId int64) error {
	bbsDiscussSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Where("id = ?", bbsDiscussId).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsDiscussSql, args...))
	if err != nil || len(bbsData) == 0 {
		gogo_log.Error("server_BbsByDelete", "数据不存在", err.Error())
		return errors.New("数据不存在")
	}
	if bbsData[0]["user_id"] != userId {
		return errors.New("只有发帖人才可删除")
	}
	//进行删除
	t := time.Now().Format(time.RFC3339)
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByDelete", "事务启动失败", err.Error())
		return errors.New("内部错误1")
	}
	defer tx.Rollback()
	update := make(map[string]interface{})
	update["status"] = model.BbsDiscussByStatusForbidden
	update["time_u"] = t
	parentPath := fmt.Sprintf("%v%v-", bbsData[0]["parent_path"].(string), bbsDiscussId)

	bbsDiscussDeleteSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Where("parent_path like ?", parentPath+"%").WhereOr("id = ?", bbsDiscussId).UpdateByMap(update)
	bbsAfterUpdate, err := tx.Exec(bbsDiscussDeleteSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByDelete", "删除数据失败1", err.Error())
		return errors.New("内部错误")
	}
	bbsUpdateNumber, _ := bbsAfterUpdate.RowsAffected()
	//动态评论数-1
	bbsUpdate, arge := sql_aid.PgTable(model.TableByBbs).Where("id = ?", bbsData[0]["bbs_id"].(int64)).Dec("discuss_number", bbsUpdateNumber).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsUpdate, arge...)
	if err != nil {
		gogo_log.Error("server_BbsByDelete", "数据不存在", err.Error())
		return errors.New("数据不存在")
	}

	tx.Commit()
	return nil
}

// BbsDiscussByPraiseInc 动态评论点赞+1
func BbsDiscussByPraiseInc(userId int64, bbsDiscussId int64) error {
	//检测是否已经点赞
	bbsDiscussSql, args := sql_aid.PgTable(model.TableByBbsDiscussPraise).Where("user_id = ?", userId).Where("bbs_discuss_id  = ?", bbsDiscussId).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsDiscussSql, args...))
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "动态评论点赞查询错误", err.Error())
		return errors.New("数据不存在")
	}
	if len(bbsData) >= 1 {
		return errors.New("您已经赞过，无需重复点击")
	}
	//进行点赞操作
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "事务错误", err.Error())
		return errors.New("执行失败")
	}
	defer tx.Rollback()
	t := time.Now().Format(time.RFC3339)
	bbsDiscussInsert := make(map[string]interface{})
	bbsDiscussInsert["user_id"] = userId
	bbsDiscussInsert["bbs_discuss_id"] = bbsDiscussId
	bbsDiscussInsert["time_c"] = t
	bbsDiscussInsertSql, args := sql_aid.PgTable(model.TableByBbsDiscussPraise).InsertByMap(bbsDiscussInsert)
	_, err = tx.Exec(bbsDiscussInsertSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	//对应动态点赞数+1
	bbsDiscussIncSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Where("id = ?", bbsDiscussId).Inc("praise_number", 1).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsDiscussIncSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseInc", "对应评论动态点赞数+1失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// BbsDiscussByPraiseDec 动态评论点赞-1
func BbsDiscussByPraiseDec(userId int64, bbsDiscussId int64) error {
	//检测是否已经点赞
	bbsDiscussSql, args := sql_aid.PgTable(model.TableByBbsDiscussPraise).Where("user_id = ?", userId).Where("bbs_discuss_id  = ?", bbsDiscussId).ToSql()
	bbsDiscussData, err := sql_aid.DataToMap(model.PgSql.Query(bbsDiscussSql, args...))
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "评论动态点赞查询错误", err.Error())
		return errors.New("数据不存在")
	}
	if len(bbsDiscussData) == 0 {
		return errors.New("您还未点赞，无需取消")
	}
	//进行点赞操作
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "事务错误", err.Error())
		return errors.New("执行失败")
	}
	defer tx.Rollback()
	bbsInsertSql, args := sql_aid.PgTable(model.TableByBbsDiscussPraise).Where("user_id = ?", userId).Where("bbs_discuss_id = ?", bbsDiscussId).Delete()
	_, err = tx.Exec(bbsInsertSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "删除失败", err.Error())
		return errors.New("执行失败")
	}
	//对应动态点赞数-1
	bbsDiscussIncSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Where("id = ?", bbsDiscussId).Dec("praise_number", 1).UpdateByMap(make(map[string]interface{}))
	_, err = tx.Exec(bbsDiscussIncSql, args...)
	if err != nil {
		gogo_log.Error("server_BbsByPraiseDec", "-1操作失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// BbsByList 动态列表
func BbsByList(userId int64, typeId int64, search string, page string, pageSize string) []map[string]interface{} {

	bbsSql, args := sql_aid.PgTable(model.TableByBbs).Field("id", "title", "content", "discuss_number", "praise_number", "time_c", "user_id").Where("status = ?", model.BbsByStatusNormal).WhereRaw(func(query *sql_aid.PgQuery, i ...interface{}) {
		if i[0].(string) !=  "" { //用户昵称
			query.Where("title like ? ", "%"+i[0].(string)+"%")
		}
		if i[1].(int64) != 0 { //类型
			query.Where("type_id = ?", i[1])
		}
	},search, typeId).OrderBy("id desc").PageSize(page, pageSize).ToSql()
	data, err := sql_aid.DataToMap(model.PgSql.Query(bbsSql, args...))
	if err != nil {
		gogo_log.Error("server_BbsByList", "查询失败", err.Error())
		return []map[string]interface{}{}
	}
	bbsIdsById := util.ArrayField(&data, "id")
	//id进行拼接
	bbsIdsByString := ""
	for k, v := range bbsIdsById {
		if k == 0{
			bbsIdsByString = fmt.Sprintf("%v", v)
		}else{
			bbsIdsByString = fmt.Sprintf("%v,%v", bbsIdsByString, v)
		}
	}
	//点赞人
	praiseSql := fmt.Sprintf("select * from (select *,row_number() over (partition by bbs_id order by user_id desc) as row_id from self_bbs_praise where bbs_id in( %v )) as t where t.row_id <= 5", bbsIdsByString)
	praiseData, _ := sql_aid.DataToMap(model.PgSql.Query(praiseSql))
	//查找对应的评论
	discussSql := fmt.Sprintf("select * from (select *,row_number() over (partition by bbs_id order by parent_path desc) as row_id from self_bbs_discuss where status = %v and parent_id = %v and bbs_id in( %v )) as t where t.row_id <= 5", model.BbsDiscussByStatusNormal, 0, bbsIdsByString)

	discussData, _ := sql_aid.DataToMap(model.PgSql.Query(discussSql))
	//查找对应的图片
	bbsPictureData := model.BbsPictureByBbsIdToData(&bbsIdsById)
	//查找用户信息
	//组装所有用户ID并获取用户信息
	bbsIdsByUserId := util.ArrayField(&data, "user_id")
	praiseDataByUser := util.ArrayField(&praiseData, "user_id")                  //获取点赞表中所有用户ID
	discussDataToUserId := util.ArrayField(&discussData, "user_id")              //获取评论用户ID
	discussDataToParentUserId := util.ArrayField(&discussData, "parent_user_id") //获取评论父级用户ID
	userdataByCol := model.UserByIdsToDataP(&bbsIdsByUserId, &praiseDataByUser, &discussDataToUserId, &discussDataToParentUserId)
	//点赞数据对应的用户ID组装
	praiseUser := make([]map[string]interface{}, 0)
	for _, praiseVal := range praiseData {
		if userdataByCol[praiseVal["user_id"].(int64)] != nil {
			praiseForUserData := make(map[string]interface{})
			praiseForUserData["id"] = userdataByCol[praiseVal["user_id"].(int64)]["id"]
			praiseForUserData["avatar"] = userdataByCol[praiseVal["user_id"].(int64)]["avatar"]
			praiseForUserData["nick"] = userdataByCol[praiseVal["user_id"].(int64)]["nick"]
			praiseForUserData["bbs_id"] = praiseVal["bbs_id"].(int64)
			praiseUser = append(praiseUser, praiseForUserData)
		}
	}

	//评论数据对应用户ID组装
	for _, discussDataValue := range discussData {
		if userdataByCol[discussDataValue["user_id"].(int64)] != nil {
			discussDataValue["user"] = userdataByCol[discussDataValue["user_id"].(int64)]
		}
		if userdataByCol[discussDataValue["parent_user_id"].(int64)] != nil {
			discussDataValue["parent_user"] = userdataByCol[discussDataValue["parent_user_id"].(int64)]
		}
	}
	praiseDataToColumnByBbsId := util.ArrayColumnGroupByInt64(&praiseUser, "bbs_id")   //点赞相同的bbs_id放到一起
	discussDataToColumnByBbsId := util.ArrayColumnGroupByInt64(&discussData, "bbs_id") //评论相同的bbs_id放到一起

	for _, datav := range data {
		//用户信息
		if userdataByCol[datav["user_id"].(int64)] != nil {
			datav["user"] = userdataByCol[datav["user_id"].(int64)]
		}else{
			datav["user"] = map[string]interface{}{}
		}
		//点赞数据拼接
		if praiseDataToColumnByBbsId[datav["id"].(int64)] != nil {
			datav["praise"] = praiseDataToColumnByBbsId[datav["id"].(int64)]
		}else{
			datav["praise"] = []interface{}{}
		}
		//评论数据拼接
		if discussDataToColumnByBbsId[datav["id"].(int64)] != nil {
			datav["discuss"] = discussDataToColumnByBbsId[datav["id"].(int64)]
		}else{
			datav["discuss"] = []interface{}{}
		}
		//图片拼接
		if bbsPictureData[datav["id"].(int64)] != nil {
			datav["pictures"] = bbsPictureData[datav["id"].(int64)]
		}else{
			datav["pictures"] = []interface{}{}
		}
	}
	return data
}

// BbsByInfo 动态详情
func BbsByInfo(userId int64, bbsId int64) (map[string]interface{}, error) {
	bbsSql, arge := sql_aid.PgTable(model.TableByBbs).Where("id = ?", bbsId).ToSql()
	bbsData, err := sql_aid.DataToMap(model.PgSql.Query(bbsSql, arge...))
	if err != nil || len(bbsData) == 0 {
		gogo_log.Error("server_BbsByDelete", "数据不存在", err.Error())
		return nil, errors.New("数据不存在")
	}
	bbsPictureSql,args := sql_aid.PgTable(model.TableByBbsPicture).Where("bbs_id = ?",bbsData[0]["id"]).ToSql()
	bbsPictureData, _ := sql_aid.DataToMap(model.PgSql.Query(bbsPictureSql,args...))
	http := os.Getenv("SERVER_HTTP")
	for _, datum := range bbsPictureData {
		if datum["picture"] != nil {
			datum["picture"] = util.IsPicturePath(datum["picture"].(string),http)
		}
	}
	bbsData[0]["pictures"] = bbsPictureData
	//用户头像
	bbsUserData ,err := model.UserByInfo(bbsData[0]["user_id"].(int64))
	if err != nil {
		bbsData[0]["user"] = map[string]interface{}{}
	}else{
		bbsData[0]["user"] = bbsUserData
	}

	return bbsData[0], nil
}

//// BbsDiscussByOneLevelList 动态一级分类
//func BbsDiscussByOneLevelList(userId int64, bbsId int64, page string) []map[string]interface{} {
//	//查找动态先顶级评论
//	discussSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Field("id", "bbs_id", "user_id", "content", "praise_number", "parent_id", "time_c").Where("bbs_id = ?", bbsId).Where("parent_id = ?", 0).OrderBy("id desc").PageSize(page, "10").ToSql()
//	discussData, _ := sql_aid.DataToMap(model.PgSql.Query(discussSql, args...))
//	if len(discussData) <= 0 {
//		return nil
//	}
//	//把动态ID拼接起来找子集数据
//	discussDataByIds := util.ArrayField(&discussData, "id")
//	bbsIdsByString := ""
//	for k, v := range discussDataByIds {
//		if k == 0 {
//			bbsIdsByString = fmt.Sprintf("%v", v)
//		} else {
//			bbsIdsByString = fmt.Sprintf("%v,%v", bbsIdsByString, v)
//		}
//	}
//	discussSqlString := fmt.Sprintf("select * from (select id,content,praise_number,parent_id,parent_user_id,time_c,user_id,super_parent_id,row_number() over (partition by super_parent_id order by id desc) as row_id from self_bbs_discuss where super_parent_id in( %v )) as t where t.row_id <= 5 ", bbsIdsByString)
//	discussChildData, _ := sql_aid.DataToMap(model.PgSql.Query(discussSqlString))
//	//组装所有用户ID并获取用户信息
//	discussDataByUserIds := util.ArrayField(&discussData, "user_id")
//	discussDataByChildUserIds := util.ArrayField(&discussChildData, "user_id")
//	discussDataByParentUserIds := util.ArrayField(&discussChildData, "parent_user_id")
//	//根据用户ID获取用户昵称和头像
//	userdataByKeyUserId := model.UserByIdsToData(&discussDataByUserIds, &discussDataByChildUserIds, &discussDataByParentUserIds)
//	for _, discussChildValue := range discussChildData { //组装品论里面的信息
//		if userdataByKeyUserId[discussChildValue["user_id"].(int64)] != nil {
//			discussChildValue["user"] = userdataByKeyUserId[discussChildValue["user_id"].(int64)]
//		}
//		if userdataByKeyUserId[discussChildValue["parent_user_id"].(int64)] != nil {
//			discussChildValue["parent_user"] = userdataByKeyUserId[discussChildValue["parent_user_id"].(int64)]
//		}
//	}
//	//相同的动态评论放到一起
//	discussChildDataByGroupParentId := util.ArrayColumnGroupByInt64(&discussChildData, "super_parent_id")
//	for _, discussDataValue := range discussData { //组装论坛里面的信息
//		if discussChildDataByGroupParentId[discussDataValue["id"].(int64)] != nil {
//			discussDataValue["child_discuss"] = discussChildDataByGroupParentId[discussDataValue["id"].(int64)]
//		}
//		if userdataByKeyUserId[discussDataValue["user_id"].(int64)] != nil {
//			discussDataValue["user"] = userdataByKeyUserId[discussDataValue["user_id"].(int64)]
//		}
//	}
//	return discussData
//}
//
//// BbsDiscussByTwoLevelList 动态二级分类
//func BbsDiscussByTwoLevelList(userId int64, parentId int64, page string) []map[string]interface{} {
//	discussSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Field("id", "bbs_id", "user_id", "content", "praise_number", "parent_id", "time_c", "parent_user_id").Where("super_parent_id = ?", parentId).OrderBy("id desc").PageSize(page, "10").ToSql()
//	discussData, err := sql_aid.DataToMap(model.PgSql.Query(discussSql, args...))
//	if err != nil {
//		gogo_log.Error("server_BbsDiscussByTwoLevelList", "查询错误", err.Error())
//		return nil
//	}
//	//组装所有用户ID并获取用户信息
//	discussDataByChildUserIds := util.ArrayField(&discussData, "user_id")
//	discussDataByParentUserIds := util.ArrayField(&discussData, "parent_user_id")
//	userdataByCol := model.UserByIdsToData(&discussDataByChildUserIds, &discussDataByParentUserIds)
//	for _, discussSqlValue := range discussData {
//		if userdataByCol[discussSqlValue["user_id"].(int64)] != nil {
//			discussSqlValue["user"] = userdataByCol[discussSqlValue["user_id"].(int64)]
//		}
//		if userdataByCol[discussSqlValue["parent_user_id"].(int64)] != nil {
//			discussSqlValue["parent_user"] = userdataByCol[discussSqlValue["parent_user_id"].(int64)]
//		}
//	}
//	return discussData
//}

// BbsDiscussByList 动态评论列表
func BbsDiscussByList(userId int64, bbsId int64, parentId int64, page string) []map[string]interface{} {
	discussSql, args := sql_aid.PgTable(model.TableByBbsDiscuss).Field("id", "bbs_id", "user_id", "content", "praise_number", "parent_id", "time_c", "parent_user_id").Where("status = ?", model.BbsDiscussByStatusNormal).Where("parent_id = ?", parentId).Where("bbs_id = ?", bbsId).OrderBy("id desc").PageSize(page, "10").ToSql()
	discussData, err := sql_aid.DataToMap(model.PgSql.Query(discussSql, args...))
	if err != nil {
		gogo_log.Error("server_BbsDiscussByList", "查询错误", err.Error())
		return nil
	}
	//组装所有用户ID并获取用户信息
	discussDataByChildUserIds := util.ArrayField(&discussData, "user_id")
	discussDataByParentUserIds := util.ArrayField(&discussData, "parent_user_id")
	userdataByCol := model.UserByIdsToDataP(&discussDataByChildUserIds, &discussDataByParentUserIds)
	for _, discussSqlValue := range discussData {
		if userdataByCol[discussSqlValue["user_id"].(int64)] != nil {
			discussSqlValue["user"] = userdataByCol[discussSqlValue["user_id"].(int64)]
		}else{
			discussSqlValue["user"] = map[string]interface{}{}
		}
		if userdataByCol[discussSqlValue["parent_user_id"].(int64)] != nil {
			discussSqlValue["parent_user"] = userdataByCol[discussSqlValue["parent_user_id"].(int64)]
		}else{
			discussSqlValue["parent_user"] = map[string]interface{}{}
		}
	}
	return discussData
}
